package com.example.handler.excelhander;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.example.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

@Slf4j
public abstract class AbstractCellWriteHandler implements CellWriteHandler {

    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
 
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, int i, boolean b) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, int i, boolean b) {
        CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
        if (cell.getRowIndex()>= 1 && cell.getColumnIndex() == 0){
            //sheet名超过31时
            String stringCellValue = cell.getStringCellValue();
            int length = stringCellValue.length();
            if (length >31){
                log.info("name:{},len:{}",stringCellValue,length);
                stringCellValue=stringCellValue.substring(0,31);
            }

            //设置超链接
            CreationHelper helper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
            Hyperlink hyperlink = helper.createHyperlink(HyperlinkType.DOCUMENT);
            hyperlink.setAddress(stringCellValue+ CommonConstant.endValue);

            //设置底色
//            cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
//            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cell.setHyperlink(hyperlink);
        }

        //上边框线条类型
        cellStyle.setBorderTop(BorderStyle.THIN);
        //右边框线条类型
        cellStyle.setBorderRight(BorderStyle.THIN);
        //下边框线条类型
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //左边框线条类型
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cell.setCellStyle(cellStyle);
    }
}